<template>
  <div>
    <div class="title_warnning mb20">
      <i
        class="el-icon-warning-outline mr10"
      />{{ $t('message.gjhyhxfz') }}
    </div>
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clearFilters"
    />
    <div class="doumee-table-buttons df_ac">
      <el-button type="primary" @click="handleEdit()">{{ $t('message.add') }}</el-button>
    </div>
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-tableb"
    >
      <el-table-column
        align="center"
        :label="$t('message.jhid')"
        prop="id"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.jhbt')"
        prop="name"
        min-width="150"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.hyrq')"
        prop="portraitName"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.ffnr')"
        prop="grantObj"
        min-width="110"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.fsdx')"
        prop="smsSend"
        min-width="80"
        show-overflow-tooltip
      >
        <template v-slot="scope">
          <span v-if="scope.row.smsSend == '0'">{{ $t('message.shi') }}</span>
          <span v-if="scope.row.smsSend == '1'">{{ $t('message.fou') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="$t('message.hysl')"
        prop="memberNum"
        min-width="80"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.rwzt')"
        prop="name"
        min-width="80"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span v-if="scope.row.taskStatus == '0'">{{ $t('message.jxz') }}</span>
          <span v-if="scope.row.taskStatus == '1'">{{ $t('message.ywc') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="$t('message.rwsj')"
        prop="name"
        min-width="100"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <div>{{ $t('message.zxzs') }}：{{ scope.row.excuteNum }}</div>
          <div>{{ $t('message.zxcg') }}：{{ scope.row.excuteSuccessNum }}</div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="$t('message.zhzxsj')"
        prop="lastTime"
        min-width="160"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.createTime')"
        prop="createTime"
        min-width="160"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.jhzt')"
        min-width="80"
        fixed="right"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="1"
            inactive-value="0"
            @change="changeStatus(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column :label="$t('message.caozuo')" align="center" width="220" fixed="right">
        <template slot-scope="scope">
          <div class="flex-cc">
            <!-- <el-button v-if="meta.indexOf('NoviceHelpEdit') > -1" v-preventReClick type="text" @click="handleEdit(scope.row)">{{ $t('message.edit') }}</el-button> -->
            <el-button
              v-preventReClick
              type="text"
              @click="handleCoupon(scope.row)"
            >{{ $t('message.fsjl') }}</el-button>
            <el-button
              v-preventReClick
              type="text"
              @click="handleDetail(scope.row)"
            >{{ $t('message.xianqing') }}</el-button>
            <el-button
              v-if="scope.row.status == '0'"
              v-preventReClick
              type="text"
              @click="handleEdit(scope.row)"
            >{{ $t('message.edit') }}</el-button>
            <el-button
              v-preventReClick
              type="text"
              style="color: red"
              @click="handleDel(scope.row.id)"
            >{{ $t('message.del') }}</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <Paginations
      :total="totalCount"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
    <Edit
      v-if="isShowEdit"
      ref="EditRef"
      @openPortrayal="handleAddP"
      @success="getList()"
      @close="isShowEdit = false"
    />
    <SendCoupomRecord
      v-if="isShowCoupon"
      ref="DirectedCouponRef"
      @close="isShowCoupon = false"
      @openPortrayal="handleAddP"
      @success="getList()"
    />
    <Detail v-if="isShowDetail" ref="DetailRef" @close="isShowDetail = false" />
  </div>
</template>

<script>
import {
  marketingPlanList,
  marketingPlanDel,
  marketingPlanUpdate
} from '@/api'
import { queryFormConfig } from './config'
import SendCoupomRecord from '@/views/application/SendCoupomRecord/record'

import Edit from './Edit.vue'
import Detail from './Detail.vue'
export default {
  components: {
    Edit,
    Detail,
    SendCoupomRecord
  },
  data() {
    return {
      queryFormConfig,
      cgList: [],
      pagination: {
        page: 1,
        rows: 10
      },
      list: [],
      totalCount: 0,
      listLoading: false,
      meta: [],
      filters: {},

      isShowEdit: false,
      isShowCoupon: false,
      isShowDetail: false
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    this.getList(1)
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  activated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
    if (this.$route.query.renovate && this.$route.query.renovate === '1') {
      this.getList()
    }
  },
  methods: {
    changeStatus(row) {
      marketingPlanUpdate({
        param: {
          id: row.id,
          status: row.status
        }
      }).then(res => {
        this.$message.success(this.$t('message.ztgxcg'))
      }, () => {
        this.getList()
      })
    },
    handleDetail(row) {
      this.isShowDetail = true
      this.$nextTick(() => {
        this.$refs.DetailRef.isShowModal = true
        this.$refs.DetailRef.getDetail(row.id)
      })
    },
    handleCoupon(row) {
      this.isShowCoupon = true
      this.$nextTick(() => {
        this.$refs.DirectedCouponRef.isShowModal = true
        this.$refs.DirectedCouponRef.agcId = row.agcId
        this.$refs.DirectedCouponRef.getList()
      })
    },
    handleEdit(row) {
      this.isShowEdit = true
      if (row && row.id) {
        this.$nextTick(() => {
          this.$refs.EditRef.getDetail(row.id)
        })
      } else {
        this.$nextTick(() => {
          this.$refs.EditRef.changeType('1')
        })
      }
      this.$nextTick(() => {
        this.$refs.EditRef.isShowModal = true
      })
    },
    handleDel(id) {
      const that = this
      this.$confirm(this.$t('message.scyxjhtc'), this.$t('message.ts'), {
        confirmButtonText: this.$t('message.qd'),
        cancelButtonText: this.$t('message.qx'),
        type: 'warning'
      })
        .then(() => {
          marketingPlanDel({ param: { id }}).then((res) => {
            if (res.errorCode !== '000000') return
            that.$message.success(this.$t('message.delcg'))
            that.getList(1)
          })
        })
    },
    handleAddP() {
      this.$emit('addPortrayal')
    },
    getList(page) {
      const { pagination, filters: param } = this
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      if (param.selDate && param.selDate.length > 0) {
        param.startTime = param.selDate[0]
        param.endTime = param.selDate[1]
      } else {
        param.startTime = null
        param.endTime = null
      }
      this.listLoading = true
      marketingPlanList({
        pagination,
        param
      }, sessionStorage.getItem('venueId')).then(res => {
        this.listLoading = false
        if (res.errorCode === '000000') {
          this.totalCount = res.totalCount
          this.list = res.recordList
        }
      }).catch(err => {
        this.listLoading = false
        console.log('-----', err)
      })
    },
    handleSizeChange(val) {
      this.pagination.rows = val
      this.getList(1)
    },
    // 清空筛选项
    clearFilters() {
      this.filters = {}
      this.getList(1)
    }
  }
}
</script>

<style lang="scss" scoped>
.title_warnning {
  height: 36px;
  line-height: 36px;
  background: #f0f3fa;
  border-radius: 2px;
  color: #435ebe;
  padding-left: 20px;
}
</style>
